package com.css.dzj.dzjap.modules.lawenforcesuper.controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.GeneralExtractRelationEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.GeneralWorkInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.GeneralService;
import com.css.dzj.dzjap.modules.lawenforcesuper.view.GeneralExtractVo;
import com.css.dzj.dzjap.modules.lawenforcesuper.view.GeneralVo;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/drawgeneral")
public class GeneralController {

    @Autowired
    private GeneralService generalService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    /***
     * 抽取一般工程
     * @param request
     * @param generalVo
     * @return
     */
    @PostMapping("/savegeneral")
    public R drawGeneral(HttpServletRequest request,@RequestBody GeneralVo generalVo){
        String s = "";
        try {
            R r = generalService.saveGeneral(generalVo, request);
            if ("500".equals(r.get("code").toString())) {
                return R.error(r.get("msg").toString());
            }
            s = JSON.toJSONString(generalVo);
            sysAuditService.saveSuccessAudit("添加成功", Constant.DRAWGENERAL, Constant.PT, Constant.ADD, s, request);
            return R.ok().put("taskId", r.get("taskId"));
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.DRAWGENERAL, Constant.YZ, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 查询列表
     * @param title
     * @param pageNo
     * @param pageSize
     * @param request
     * @return
     */
    @GetMapping("/querydraw")
    public R queryDraw(@RequestParam(required = false) String title,
                       @RequestParam(required = false) Integer pageNo,
                       @RequestParam(required = false) Integer pageSize,
                       HttpServletRequest request){
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<GeneralWorkInfoEntity> results = generalService.getpage(pageRequest, title, request);
            s = JSON.toJSONString(results);
            sysAuditService.saveSuccessAudit("查询成功", Constant.DRAWGENERAL, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("data", results);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.DRAWGENERAL, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }
    /**
     *查询关联表信息
     */
    @GetMapping("/queryRelationById")
    public R queryRelationById(@RequestParam(required = true)String taskId,Integer pageNo,Integer pageSize){
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<GeneralExtractRelationEntity> results = generalService.queryRelationById(taskId, pageRequest);
            s = JSON.toJSONString(results);
            sysAuditService.saveSuccessAudit("查询成功", Constant.DRAWGENERAL, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("data", results);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("修改投诉举报失败", Constant.DRAWGENERAL, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 按照任務ID查詢抽取的項目列表
     * @param taskId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryById")
    public R queryById(@RequestParam(required = true)String taskId,Integer pageNo,Integer pageSize){
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<GeneralExtractVo> results = generalService.queryById(taskId, pageRequest);
            List<GeneralWorkInfoEntity> generalWorkInfoEntities = generalService.findByTaskId(taskId);
            //TODO
            s = JSON.toJSONString(results);
            sysAuditService.saveSuccessAudit("查询成功", Constant.DRAWGENERAL, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("data", results).put("page", generalWorkInfoEntities);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.DRAWGENERAL, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 拟制公告
     * @param apPublicEntity
     * @return
     */
    @PostMapping("/addPub")
    public R addPub(@RequestBody ApPublicEntity apPublicEntity, HttpServletRequest request) {
        String s = "";
        try {
            if (StringUtils.isEmpty(apPublicEntity)) {
                return R.error("公告为空");
            }
            generalService.addPulic(apPublicEntity, request);
            s = JSON.toJSONString(apPublicEntity);
            sysAuditService.saveSuccessAudit("添加成功", Constant.DRAWGENERAL, Constant.PT, Constant.ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.ALLSUPERVISE, Constant.YZ, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }


}
